﻿

//

using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using LaserMark.Components.Services;
using LaserMark.Control.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LaserMark.Components;
public partial class IOPointStatus
{
    [Inject]
    [NotNull]
    private IControl? Control { get; set; }

    [Inject]
    [NotNull]
    private IGlobalStatus? GlobalStatus { get; set; }

    private Color AppRun { get; set; } = Color.Secondary;

    private Color AppPause { get; set; } = Color.Secondary;

    private Color Stop { get; set; } = Color.Secondary;

    private Color AutoManual { get; set; } = Color.Secondary;

    private Color Light { get; set; } = Color.Secondary;

    private Color Drive { get; set; } = Color.Secondary;

    private Color Grate { get; set; } = Color.Secondary;

    private Color GlassStop { get; set; } = Color.Secondary;

    private Color GlassTakeOff { get; set; } = Color.Secondary;

    private Color ManualTrans { get; set; } = Color.Secondary;

    private Color StopCallBack { get; set; } = Color.Secondary;

    private Color StopIndication { get; set; } = Color.Secondary;

    private Color PauseIndication { get; set; } = Color.Secondary;

    private Color RunIndication { get; set; } = Color.Secondary;

    private Color TransArrow { get; set; } = Color.Secondary;

    private Color TransReserve { get; set; } = Color.Secondary;

    private Color AirCylinder { get; set; } = Color.Secondary;

    private Color Marked { get; set; } = Color.Secondary;

    private Color RequestOnPiece { get; set; } = Color.Secondary;

    private Color ManualOrder { get; set; } = Color.Secondary;

    private Color XHome { get; set; } = Color.Secondary;

    private Color YHome { get; set; } = Color.Secondary;

    private Color ZHome { get; set; } = Color.Secondary;


    private Color OnStateReady { get; set; } = Color.Secondary;

    [NotNull]
    private CancellationTokenSource? CancellationTokenSource { get; set; } = new();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            _ = Task.Run(async () =>
            {
                while (!CancellationTokenSource.IsCancellationRequested)
                {
                    await Task.Delay(500);
                    try
                    {
                        if (GlobalStatus.IsRunning)
                        {
                            await Control.GetDi(0, mc.MC_GPI, out int input);
                            mc.GT_GetDo(0, mc.MC_GPO, out int output);

                            await Control.GetDi(0, mc.MC_HOME, out int home);

                            var status = Convert.ToString(input, 2).PadLeft(16, '0');
                            var status1 = Convert.ToString(output, 2).PadLeft(16, '0');
                            var home1 = Convert.ToString(home, 2).PadLeft(8, '0');
                            if (status[15] == '0')
                            {
                                AppRun = Color.Danger;
                            }
                            else
                            {
                                AppRun = Color.Success;
                            }

                            if (status[14] == '0')
                            {
                                AppPause = Color.Danger;
                            }
                            else
                            {
                                AppPause = Color.Success;
                            }

                            if (status[13] == '1')
                            {
                                Stop = Color.Danger;
                            }
                            else
                            {
                                Stop = Color.Success;
                            }

                            if (status[12] == '1')
                            {
                                AutoManual = Color.Danger;
                            }
                            else
                            {
                                AutoManual = Color.Success;
                            }

                            if (status[8] == '1')
                            {
                                Light = Color.Danger;
                            }
                            else
                            {
                                Light = Color.Success;
                            }

                            if (status[6] == '0')
                            {
                                Drive = Color.Danger;
                            }
                            else
                            {
                                Drive = Color.Success;
                            }

                            if (status[5] == '1')
                            {
                                Grate = Color.Danger;
                            }
                            else
                            {
                                Grate = Color.Success;
                            }

                            if (status[4] == '1')
                            {
                                GlassStop = Color.Danger;
                            }
                            else
                            {
                                GlassStop = Color.Success;
                            }

                            if (status[2] == '1')
                            {
                                GlassTakeOff = Color.Danger;
                            }
                            else
                            {
                                GlassTakeOff = Color.Success;
                            }

                            if (status[1] == '1')
                            {
                                OnStateReady = Color.Success;
                            }
                            else
                            {
                                OnStateReady = Color.Danger;
                            }

                            if (status[0] == '1')
                            {
                                ManualTrans = Color.Danger;
                            }
                            else
                            {
                                ManualTrans = Color.Success;
                            }

                            if (status1[15] == '1')
                            {
                                StopCallBack = Color.Danger;
                            }
                            else
                            {
                                StopCallBack = Color.Success;
                            }

                            if (status1[14] == '0')
                            {
                                StopIndication = Color.Danger;
                            }
                            else
                            {
                                StopIndication = Color.Success;
                            }

                            if (status1[13] == '1')
                            {
                                PauseIndication = Color.Success;
                            }
                            else
                            {
                                PauseIndication = Color.Danger;
                            }

                            if (status1[12] == '1')
                            {
                                RunIndication = Color.Success;
                            }
                            else
                            {
                                RunIndication = Color.Danger;
                            }

                            if (status1[8] == '1')
                            {
                                TransReserve = Color.Success;
                            }
                            else
                            {
                                TransReserve = Color.Danger;
                            }

                            if (status1[7] == '1')
                            {
                                TransArrow = Color.Success;
                            }
                            else
                            {
                                TransArrow = Color.Danger;
                            }

                            if (status1[6] == '1')
                            {
                                AirCylinder = Color.Success;
                            }
                            else
                            {
                                AirCylinder = Color.Danger;
                            }

                            if (status1[5] == '1')
                            {
                                Marked = Color.Success;
                            }
                            else
                            {
                                Marked = Color.Danger;
                            }

                            if (status1[4] == '1')
                            {
                                RequestOnPiece = Color.Success;
                            }
                            else
                            {
                                RequestOnPiece = Color.Danger;
                            }

                            if (status1[3] == '1')
                            {
                                ManualOrder = Color.Success;
                            }
                            else
                            {
                                ManualOrder = Color.Danger;
                            }

                            if (home1[6] == '1')
                            {
                                XHome = Color.Danger;
                            }
                            else
                            {
                                XHome = Color.Success;
                            }

                            if (home1[5] == '1')
                            {
                                YHome = Color.Danger;
                            }
                            else
                            {
                                YHome = Color.Success;
                            }

                            if (home1[4] == '1')
                            {
                                ZHome = Color.Danger;
                            }
                            else
                            {
                                ZHome = Color.Success;
                            }

                            await InvokeAsync(StateHasChanged);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            });

        }
    }
}
